/***********************************************************************************************************
* TNA - The Network Auditor
* Copyright © 2012  Jeff Scaparra -- SPAWAR Systems Center LANT
*
* This file is a part of TNA.
************************************************************************************************************/

#include "packetsniffer.h"
#include <QFile>
#include <QTextStream>
#include <QStringList>

PacketSniffer::PacketSniffer()
{
  enableTcpServicePortDiscoverer_ = true;
  sniffer_.setInputDevice( "eth0" ); //default device
}

void PacketSniffer::setDevice(const std::string &device)
{
  sniffer_.setInputDevice( device );
}

void PacketSniffer::setConfFile(const std::string &conf)
{
  confFile_ = conf;
}

std::string PacketSniffer::confFile() const
{
  return confFile_;
}

void PacketSniffer::start()
{
  sniffer_.start();
  while( true ) //do forever
  {
    Packet p = sniffer_.popPacket();

    if( enableTcpServicePortDiscoverer_ ) //not null
    {
      tcpServicePortDiscoverer_.processPacket(p);
    }

  }
  sniffer_.stop(); //won't ever get here unless we modify the loop above.
}

void PacketSniffer::readConf()
{
  QFile file("in.txt");
  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
      return;

  QTextStream in(&file);
  while (!in.atEnd()) {
      QString line = in.readLine();
      line = processLine(line);
  }
}

QString PacketSniffer::processLine(const QString &line)
{
  return line.split('#')[0].simplified();
}

QString PacketSniffer::commentRemover(const QString &line)
{
  QStringList list = line.split('#'); //# is the comment character in the conf
  return list[0];
}

QString PacketSniffer::whiteSpaceRemover(const QString &line)
{
  return line.simplified(); //:) that was easy
}
